// 修改密码
bangkeapp.controller('updatepwdctrl', 
["$scope", "$state", "safeService", "userinfoService",
	function($scope, $state, safeService, userinfoService) {
		 
		$scope.userinfo = userinfoService.getUser();
		if (undefined != $scope.userinfo.memberid && '' != $scope.userinfo.memberid) {
			$scope.member = {};
			$scope.member.password = "";
			$scope.member.memberid = $scope.userinfo.memberid;
			$scope.member.newpassword = "";
			$scope.member.repassword = "";
			// 修改密码
			$scope.updatepwd = function(){
				// 校验
				if ("" == $scope.member.password){
					alert("旧密码不能为空!");
					return false;
				}
				if ("" == $scope.member.newpassword){
					alert("新密码不能为空!");
					return false;
				}
				if ("" == $scope.member.repassword){
					alert("确认密码不能为空!");
					return false;
				}
				if ($scope.member.newpassword.length < 8) {
					alert("密码必须大于等于8位!");
					return false;
				}
				if ($scope.member.repassword != $scope.member.newpassword) {
					alert("确认密码不一致!");
					return false;
				}
				if ($scope.member.password == $scope.member.newpassword) {
					alert("新密码与旧密码一致!");
					return false;
				}
				safeService.updatepwd($scope.member).then(function(result) {
					var data = result;
					if(data.Response.Header.ResponseCode == "M") {
						alert("修改成功!");
						$state.go('bangke.mycenter', {});
					} else {
						alert(data.Response.Header.MsgList[0].MsgContent);
					}
				}, function(error) {
					console.log("error!");
				}, function(progress) {
	
				});
			}
		}
	}
]);

// 修改支付密码
bangkeapp.controller('updatepaypwdctrl',  
["$scope", "$state", "safeService", "userinfoService", "$ionicModal", "$ionicPopover", "$ionicLoading",
	function($scope, $state, safeService, userinfoService, $ionicModal, $ionicPopover, $ionicLoading) {
		
		$scope.password = {};
		
		// 确认密码输入模块
		$ionicPopover.fromTemplateUrl('resetpaypwdre.html', {
			scope: $scope
		}).then(function(popover) {
			$scope.repassword = popover;
		});
		
        // 点击下一步按钮, 关闭输入旧密码模态框
        $scope.nextstep1 = function(){
        		if (undefined != $scope.password.oldSecurepwd 
        				&& '' != $scope.password.oldSecurepwd
        				&& 6 == $scope.password.oldSecurepwd.length
        				&& $scope.IsNum($scope.password.oldSecurepwd)) {
        			
				// 新密码输入模块
				$ionicPopover.fromTemplateUrl('resetpaypwdnew.html', {
					scope: $scope
				}).then(function(popover) {
					$scope.newpassword = popover;
        				$scope.newpassword.show();
				});
        			$scope.tips = "";
        		} else {
        			$scope.password.oldSecurepwd = '';
        			angular.element(document.getElementById("oldSecurepwd")).val("");
        			$scope.tips = "密码格式错误!";
        		}
        }
        
        // 点击下一步按钮, 跳转到确认密码模块
        $scope.nextstep2 = function(){
        		if (undefined != $scope.password.securepwd 
        				&& '' != $scope.password.securepwd
        				&& 6 == $scope.password.securepwd.length
        				&& $scope.IsNum($scope.password.securepwd)) {
				$scope.newpassword.remove();
				// 确认密码输入模块
				$ionicPopover.fromTemplateUrl('resetpaypwdre.html', {
					scope: $scope
				}).then(function(popover) {
					$scope.repassword = popover;
					$scope.repassword.show();
				});
        			$scope.tips = "";
        		} else {
        			$scope.password.securepwd = '';
        			angular.element(document.getElementById("securepwd")).val("");
        			$scope.tips = "密码格式错误!";
        		}
        }
        
        $scope.IsNum = function(num){
 			var reNum=/^\d*$/;
 			return(reNum.test(num));
		}
        
		$scope.userinfo = userinfoService.getUser();
		
		// 修改密码
		$scope.resetpaypwd = function(){
			$ionicLoading.show({
				content: 'Loading',
				animation: 'fade-in',
				showBackdrop: true,
				maxWidth: 200,
				showDelay: 0
			});
			$scope.repassword.remove();
			if (undefined != $scope.userinfo.memberid && '' != $scope.userinfo.memberid) {
			$scope.password.memberid = $scope.userinfo.memberid;
				$scope.password.isShortCode = "0";
				$scope.password.isCheckOld = "1";
				// 校验
				if (undefined ==  $scope.password.oldSecurepwd || "" == $scope.password.oldSecurepwd){
					alert("旧密码不能为空!");
					$scope.cleanoldpassword();
					return false;
				}
				if (undefined ==  $scope.password.securepwd || "" == $scope.password.securepwd){
					alert("新密码不能为空!");
					$scope.cleanoldpassword();
					return false;
				}
				if (undefined ==  $scope.password.surepwd || "" == $scope.password.surepwd){
					alert("确认密码不能为空!");
					$scope.cleanoldpassword();
					return false;
				}
				if ($scope.password.securepwd.length < 6) {
					alert("密码必须大于等于6位!");
					$scope.cleanoldpassword();
					return false;
				}
				if ($scope.password.surepwd != $scope.password.securepwd) {
					alert("确认密码不一致!");
					$scope.cleanoldpassword();
					return false;
				}
				if ($scope.password.oldSecurepwd == $scope.password.securepwd) {
					alert("新密码与旧密码一致!");
					$scope.cleanoldpassword();
					return false;
				}
				safeService.saveorupdatesecurepwd($scope.password).then(function(result) {
					var data = result;
					if("01" == data.body.handleStatus 
						&& data.body.memberid == $scope.password.memberid
						&& 'Y' == data.body.operateStatus ) {
						alert("修改成功!");
						$state.go('bangke.mycenter', {});
					} else {
						alert(data.header.MsgList[0].MsgContent);
						$scope.cleanoldpassword();
					}
					$ionicLoading.hide();
				}, function(error) {
					alert("修改失败!");
					$scope.password = {};
					$ionicLoading.hide();
				}, function(progress) {
				});
			}
		}
		
		$scope.cancel = function(){
			$scope.repassword.remove();
			$scope.newpassword.remove();
			$ionicLoading.hide();
			$state.go('bangke.mycenter', {});
		}
		
		$scope.cleanoldpassword = function(){
			$scope.password = {};
			$ionicLoading.hide();
		}
	}
]);

// 重置支付密码
bangkeapp.controller('resetpaypwdctrl', 
["$scope", "$state", "safeService", "userinfoService", "$ionicModal", "$interval", "memberService", "$ionicLoading", "$ionicPopover",
	function($scope, $state, safeService, userinfoService, $ionicModal, $interval, memberService, $ionicLoading, $ionicPopover) {
		
		$scope.password = {};
		
        // 点击下一步按钮, 关闭输入旧密码模态框
        $scope.nextstep1 = function(){
        		if (undefined != $scope.password.shortCode 
        				&& '' != $scope.password.shortCode
        				&& 4 == $scope.password.shortCode.length
        				&& $scope.IsNum($scope.password.shortCode)) {
        			
        			// 新密码输入模块
				$ionicPopover.fromTemplateUrl('resetpaypwdnew.html', {
					scope: $scope
				}).then(function(popover) {
					$scope.newpassword = popover;
					
					// 新密码输入模块
					$ionicPopover.fromTemplateUrl('resetpaypwdnew.html', {
						scope: $scope
					}).then(function(popover) {
						$scope.newpassword = popover;
	        				$scope.newpassword.show();
					});
				});
        			$scope.tips = "";
        		} else {
        			$scope.password.shortCode = '';
        			$scope.tips = "验证码错误!";
        		}
        }
        
        // 点击下一步按钮, 跳转到确认密码模块
        $scope.nextstep2 = function(){
        		if (undefined != $scope.password.securepwd 
        				&& '' != $scope.password.securepwd
        				&& 6 == $scope.password.securepwd.length
        				&& $scope.IsNum($scope.password.securepwd)) {
        			
        			$scope.newpassword.remove();
        			
        			// 确认密码输入模块
				$ionicPopover.fromTemplateUrl('resetpaypwdre.html', {
					scope: $scope
				}).then(function(popover) {
					$scope.repassword = popover;
					$scope.repassword.show();
				});
        			
        			$scope.tips = "";
        		} else {
        			$scope.password.securepwd = '';
        			angular.element(document.getElementById("securepwd")).val("");
        			$scope.tips = "密码格式错误!";
        		}
        }
        
        $scope.IsNum = function(num){
 			var reNum=/^\d*$/;
 			return(reNum.test(num));
		}
        
		$scope.userinfo = userinfoService.getUser();
		
		$scope.text = "获取验证码";
    		$scope.initpaypwd = {};
    		
    		// 获取验证码
    		$scope.getsmscode = function(){
    			
			if (undefined == $scope.userinfo.tel || "" == $scope.userinfo.tel) {
				alert("手机号不能为空!");
				return false;
			}
			var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/; 
			if(!myreg.test($scope.userinfo.tel)) { 
			    alert('不是有效手机号！'); 
			    return false; 
			} 
			$scope.tel = $scope.userinfo.tel;
			
			$scope.n = 60;
	        $scope.text = $scope.n + "秒";
	        var btn = angular.element(document.getElementById("getsmscoderesetpaypwdbtn"));
	        btn.css({"background-color": "transparent"});
	        
	        $scope.isDisable = true;
	
	        var time = $interval(function () {
	
	            $scope.n--;
	            $scope.text = $scope.n + "秒后可重新获取";
	            if ($scope.n == 0)
	            {
	                $interval.cancel(time);
	                $scope.isDisable = false;
	                $scope.text = "获取验证码";
	                btn.css({"background-color": "#ccc"});
	            }
	
	        },1000);

    			memberService.getsmscode($scope.userinfo.tel).then(function() {
	    		}, function(error) {
	        }, function(progress) {
	        });
    		}
    		
		// 修改密码
		$scope.resetpaypwd = function(){
			$ionicLoading.show({
				content: 'Loading',
				animation: 'fade-in',
				showBackdrop: true,
				maxWidth: 200,
				showDelay: 0
			});
			$scope.repassword.remove();
			if (undefined != $scope.userinfo.memberid && '' != $scope.userinfo.memberid) {
				$scope.password.memberid = $scope.userinfo.memberid;
				$scope.password.isShortCode = "1";
				$scope.password.isCheckOld = "0";
				// 校验
				if (undefined ==  $scope.password.securepwd || "" == $scope.password.securepwd){
					alert("新密码不能为空!");
					$scope.password = {};
					$ionicLoading.hide();
					return false;
				}
				if (undefined ==  $scope.password.surepwd || "" == $scope.password.surepwd){
					alert("确认密码不能为空!");
					$scope.password = {};
					$ionicLoading.hide();
					return false;
				}
				if ($scope.password.securepwd.length < 6) {
					alert("密码必须大于等于6位!");
					$scope.password = {};
					$ionicLoading.hide();
					return false;
				}
				if ($scope.password.surepwd != $scope.password.securepwd) {
					alert("确认密码不一致!");
					$scope.password = {};
					$ionicLoading.hide();
					return false;
				}
				safeService.saveorupdatesecurepwd($scope.password).then(function(result) {
					var data = result;
					$ionicLoading.hide();
					if("01" == data.body.handleStatus 
						&& data.body.memberid == $scope.password.memberid
						&& 'Y' == data.body.operateStatus ) {
						alert("修改成功!");
						$state.go('bangke.mycenter', {});
					} else {
						alert(data.header.MsgList[0].MsgContent);
						$scope.password = {};
						$ionicLoading.hide();
					}
				}, function(error) {
					alert("修改失败!");
					$ionicLoading.hide();
					$scope.password = {};
				}, function(progress) {
				});
			}
		}
		$scope.cancel = function(){
			try {
				
				$scope.newpassword.remove();
				$scope.repassword.remove();
			} catch (e) {
				
			}
			$ionicLoading.hide();
			$state.go('bangke.mycenter', {});
		}
	}
]);